/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.communication;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.softwareinlife.vision.android.communication.model.EventModel;
import com.softwareinlife.vision.android.communication.model.PlanModel;
import com.softwareinlife.vision.android.communication.model.ScheduleModel;
import com.softwareinlife.vision.android.communication.model.UserModel;
import com.softwareinlife.vision.android.communication.model.ValueModel;
import com.softwareinlife.vision.android.communication.model.VisionModel;
import com.softwareinlife.vision.android.communication.model.VisionSoft;
import com.softwareinlife.vision.android.system.util.StringUtil;

/**
 * @author Cho, jae-young (greenfrog@softwareinlife.com)
 * @date 2009. 10. 19.
 */
public class SyncXmlParsingHandler extends DefaultHandler
{
	VisionSoft m_visionSoft;
	ValueModel m_valueModel;
	UserModel m_userModel;
	VisionModel m_visionModel;
	PlanModel m_planModel;
	ScheduleModel m_scheduleModel;
	EventModel m_eventModel;
	
	List<ValueModel> m_valueModels;
	
	boolean m_isProcessAboutValue = false;
	
	StringBuilder m_builder = new StringBuilder();

	public VisionSoft getRootItem()
	{
		return m_visionSoft;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);

		m_builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException 
	{
		super.endElement(uri, localName, name);
		
		if(true == m_isProcessAboutValue) 
		{
			m_valueModel.setDesc(m_builder.toString());
			m_isProcessAboutValue = false;
		}
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException 
	{
		super.startElement(uri, localName, name, attributes);
		
		if (true == localName.equalsIgnoreCase("visionsoft"))
		{
			m_visionSoft = new VisionSoft();
		} 
		else if (true == localName.equalsIgnoreCase("user"))
		{
			m_userModel = new UserModel();
			
			m_userModel.setUserId(getAttribute("id", attributes));
			m_userModel.setUserPassword(getAttribute("pw", attributes));
			m_userModel.setFirstName(getAttribute("firstname", attributes));
			m_userModel.setLastName(getAttribute("lastname", attributes));
			m_userModel.setNickName(getAttribute("nickname", attributes));
			m_userModel.setEMail(getAttribute("email", attributes));
			m_userModel.setClientId(getAttribute(getAttribute("clientid", attributes)));
			m_userModel.setAction(getAttribute("a", attributes));
			
			m_visionSoft.AddUserModel(m_userModel);
		}
		else if (true == localName.equalsIgnoreCase("value"))
		{
			m_valueModel = new ValueModel();
			m_userModel.AddModel(m_valueModel);
		}
		else if (true == localName.equalsIgnoreCase("vision"))
		{
			m_visionModel = new VisionModel();
			
			m_visionModel.setIds(getAttribute(getAttribute("id", attributes)));
			m_visionModel.setClientId(getAttribute(getAttribute("clientid", 
					attributes)));
			m_visionModel.setTitle(getAttribute("title", attributes));
			m_visionModel.setStatement(getAttribute("statement", attributes));
			m_visionModel.setExpect(getAttribute("expect", attributes));
			m_visionModel.setBlueprint(getAttribute("blueprint", attributes));
			//m_visionModel(getAttribute("color", attributes));
			m_visionModel.setAction(getAttribute("a", attributes));
			
			m_visionSoft.AddVisionModel(m_visionModel);
		}
		else if (true == localName.equalsIgnoreCase("plan"))
		{
			m_planModel = new PlanModel();
			
			m_planModel.setIds(getAttribute(getAttribute("id", attributes)));
			m_planModel.setClientId(getAttribute(getAttribute("clientid", 
					attributes)));
			m_planModel.setTitle(getAttribute("title", attributes));
			m_planModel.setDesc(getAttribute("description", attributes));
			m_planModel.setAction(getAttribute("a", attributes));
			
			m_visionSoft.AddPlanModel(m_planModel);
		}
		else if (true == localName.equalsIgnoreCase("schedule"))
		{
			m_scheduleModel = new ScheduleModel();
			
			m_scheduleModel.setIds(getAttribute(getAttribute("id", attributes)));
			m_scheduleModel.setClientId(getAttribute(getAttribute("clientid", 
					attributes)));
			m_scheduleModel.setTitle(getAttribute("title", attributes));
			m_scheduleModel.setDesc(getAttribute("description", attributes));
			m_scheduleModel.setStartDate(StringUtil.toDate(
					getAttribute("startDate", attributes)));
			m_scheduleModel.setStartDate(StringUtil.toDate(
					getAttribute("endDate", attributes)));
			
			m_visionSoft.AddScheduleModel(m_scheduleModel);
		}
		else if (true == localName.equalsIgnoreCase("event"))
		{
			m_eventModel = new EventModel();
			
			m_eventModel.setScheduleId(getAttribute(getAttribute("schduleid", 
					attributes)));
			m_eventModel.setStartDate(StringUtil.toDate(
					getAttribute("startDate", attributes)));
			m_eventModel.setStartDate(StringUtil.toDate(
					getAttribute("endDate", attributes)));
			
			m_visionSoft.AddEventModel(m_eventModel);
		}
		else if (true == localName.equalsIgnoreCase("visionPlan"))
		{
		
		}
		else if (true == localName.equalsIgnoreCase("planSchedule"))
		{
		
		}
	}
	
	private String getAttribute(String arg_qName, Attributes arg_attributes)
	{
		String result = arg_attributes.getValue(arg_qName);
		
		return (null == result)? "": result;
	}
	
	private int getAttribute(String arg_attrVal)
	{
		if(arg_attrVal.equalsIgnoreCase(""))
			return 0;
		else
			return Integer.parseInt(arg_attrVal);
	}
}

